package com.proscenic.oauth.server.config;

import com.proscenic.oauth.server.config.security.MyAuthorizationCodeServices;
import com.proscenic.oauth.server.vo.MyAccessTokenConverter;
import com.proscenic.oauth.server.vo.MyTokenEnhancer;
import com.proscenic.oauth.server.service.MyClientDetailsService;
import com.proscenic.oauth.server.service.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private MyUserDetailsService userDetailsService;
    @Autowired
    private MyClientDetailsService clientDetailsService;
    @Autowired
    private MyAuthorizationCodeServices authorizationCodeServices;
    @Autowired
    private MyTokenEnhancer tokenEnhancer;
    @Autowired
    private MyAccessTokenConverter accessTokenConverter;
    @Resource
    private DataSource dataSource;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //内存加载
        clients.inMemory()
                .withClient("aaa")
                .secret(passwordEncoder.encode("secret"))
                .authorizedGrantTypes(AuthorizationGrantType.AUTHORIZATION_CODE.getValue(), AuthorizationGrantType.REFRESH_TOKEN.getValue())
                .scopes("all")
                .autoApprove(true)
                .redirectUris("none")
        ;

        //自定义认证中心规则
//        clients.withClientDetails(clientDetailsService());
    }

//    @Bean
//    public ClientDetailsService clientDetailsService(){
//        return new JdbcClientDetailsService(dataSource);
//    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                //自定义授权码生成规则
                .authorizationCodeServices(authorizationCodeServices)

                //自定义用户认证规则
                .userDetailsService(userDetailsService)

                .tokenEnhancer(tokenEnhancer)
//                .accessTokenConverter(accessTokenConverter)

        ;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                //允许使用form的方式进行authentication的授权
                .allowFormAuthenticationForClients()
        ;
    }


}
